393. UTF-8 编码验证
为保证权益,题目请参考 393. UTF-8 编码验证(From LeetCode).
解决方案1
Python
python
# 393. UTF-8 编码验证
# https://leetcode-cn.com/problems/utf-8-validation/
from typing import List
class Solution:
def validUtf8(self, data: List[int]) -> bool:
def get_one_before_zero(num: int):
n = bin(num)
n = "{:0>8}".format(n[2:])
ans = 0
for i in range(8):
if n[i] == "0":
break
else:
ans += 1
return ans
def is01(num: int):
n = bin(num)
n = "{:0>8}".format(n[2:])
return n.startswith("10")
n = -1
for i in range(len(data)):
if n == -1:
n = get_one_before_zero(data[i])
if n > 4:
break
n = n - 1
else:
if not is01(data[i]):
break
n = n - 1
if n == 0:
n = -1
return n == -1
if __name__ == '__main__':
solution = Solution()
print(solution.validUtf8([250,145,145,145,145]))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46